﻿using System;
using System.Web.Mvc;

using Fuse8.ContentManagementFramework.Web.ApplicationRuntime;

namespace Fuse8.ContentManagementFramework.Administration
{
    public class CmsAdminRoutes : AreaRegistration
    {
        public const string AdminMediaFilePreviewOriginalRoute = "CmsAdminMediaFile_Preview_Original";

        public override string AreaName
        {
            get
            {
                return CmsRoute.AdministrationAreaName;
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.Routes.RouteExistingFiles = false;

            context.Routes.CmsAdministrationIgnoreRoute("admin/assets/{*pathInfo}");
            context.Routes.CmsAdministrationIgnoreRoute("admin/assets/{resource}.css");
            context.Routes.CmsAdministrationIgnoreRoute("admin/assets/{resource}.js");
            context.Routes.CmsAdministrationIgnoreRoute("{resource}.axd/{*pathInfo}");
            context.Routes.CmsAdministrationIgnoreRoute("admin/dbpatch.axd");

            context.NonCmsMapRoute(
                "Media_Code_Handler",
                "media/code/{code}",
                new { controller = "CmsBaseFrontEnd", action = "GetMediaContentByCode", code = UrlParameter.Optional },
                null,
                new string[] { "Fuse8.ContentManagementFramework.Web.ApplicationRuntime" });

            context.CmsAdministrationMapRoute(
                "Media_Id_Handler",
                "mediafile/id/{id}.{ext}",
                new { controller = "CmsBaseFrontEnd", action = "GetMediaFileById", id = UrlParameter.Optional, ext = UrlParameter.Optional },
                null,
                new string[] { "Fuse8.ContentManagementFramework.Web.ApplicationRuntime" });

            context.CmsAdministrationMapRoute(
                "CmsAdmin_Account",
                "admin/account/{action}/{id}",
                new { controller = "CmsAccount", action = "Index", id = UrlParameter.Optional },                
                new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                AdminMediaFilePreviewOriginalRoute,
                "admin/media/{fileName}",
                new { controller = "CmsMedia", action = "GetMediaFile", fileName = UrlParameter.Optional },
                new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                "CmsAdmin_GetPageControls",
                "admin/cmspage/pagecontrols/{pageId}/{templateRegionId}",
                new { controller = "CmsPage", action = "PageControls", pageId = UrlParameter.Optional, templateRegionId = UrlParameter.Optional },
                new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                "CmsAdmin_AddPageContent",
                "admin/cmspage/addcontent/{pageId}/{templateRegionId}",
                new { controller = "CmsPage", action = "AddContent", pageId = UrlParameter.Optional, templateRegionId = UrlParameter.Optional },
                new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                "CmsAdmin_AddContentModule",
                "admin/cmspage/addcontentmodule/{pageId}/{templateRegionId}",
                new { controller = "CmsPage", action = "AddContentModule", pageId = UrlParameter.Optional, templateRegionId = UrlParameter.Optional },
                new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                "CmsAdmin_AddPageSnippet",
                "admin/cmspage/addsnippet/{pageId}/{templateRegionId}",
                new { controller = "CmsPage", action = "AddSnippet", pageId = UrlParameter.Optional, templateRegionId = UrlParameter.Optional },
                new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                "CmsAdmin_AddPagePartialView",
                "admin/cmspage/addpartialview/{pageId}/{templateRegionId}",
                new { controller = "CmsPage", action = "AddPartialView", pageId = UrlParameter.Optional, templateRegionId = UrlParameter.Optional },
                new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                "CmsAdmin_AddPageActionView",
                "admin/cmspage/addactionview/{pageId}/{templateRegionId}",
                new { controller = "CmsPage", action = "AddActionView", pageId = UrlParameter.Optional, templateRegionId = UrlParameter.Optional },
                new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                "CmsAdmin_AddPageModule",
                "admin/cmspage/addmodule/{moduleid}/{pageId}/{templateRegionId}",
                new { controller = "CmsPage", action = "AddModule", moduleid = UrlParameter.Optional, pageId = UrlParameter.Optional, templateRegionId = UrlParameter.Optional },
                new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                "CmsAdmin_RemovePageControl",
                "admin/cmspage/deletepagecontrol/{pageId}/{templateRegionId}/{pageControlId}",
                new { controller = "CmsPage", action = "DeletePageControl", pageId = UrlParameter.Optional, templateRegionId = UrlParameter.Optional, pageControlId = UrlParameter.Optional },
                new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                "CmsAdmin_MoveUpPageControl",
                "admin/cmspagecontrol/moveup/{pageControlId}",
                new { controller = "CmsPageControl", action = "MoveUp", pageControlId = UrlParameter.Optional },
                new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                "CmsAdmin_MoveDownPageControl",
                "admin/cmspagecontrol/movedown/{pageControlId}",
                new { controller = "CmsPageControl", action = "MoveDown", pageControlId = UrlParameter.Optional },
                new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                "CmsAdmin_PageHistoryView",
                "admin/cmspage/view/{id}/{latestRevisionId}",
                new { controller = "CmsPage", action = "View", id = UrlParameter.Optional, latestRevisionId = UrlParameter.Optional },
                new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                "CmsAdmin_PageMove",
                "admin/cmspage/move/{id}/{moveToId}",
                new { controller = "CmsPage", action = "Move", id = UrlParameter.Optional, moveToId = UrlParameter.Optional },
                new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                "CmsAdmin_PageAnnotations",
                "admin/cmspage/annotations/{id}/",
                new { controller = "CmsPage", action = "EntityAnnotations", id = UrlParameter.Optional },
                new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute("CmsAdminSearch_Page",
                                              "admin/cmspage/edit/{id}",
                                              new {controller = "CmsPage", action = "Edit", id = UrlParameter.Optional},
                                              new {isInAdmin = new CmsAdminAreaRouteConstraint()});

            context.CmsAdministrationMapRoute("CmsAdminSearch_MediaContent",
                                              "admin/cmsmedia/view/{id}",
                                              new { controller = "CmsMedia", action = "View", id = UrlParameter.Optional },
                                              new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute("CmsContentType_MetaAdd",
                                              "admin/cmscontenttype/addmeta/{typeid}",
                                              new { controller = "CmsContentType", action = "AddMeta"},
                                              new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute("CmsContentType_MetaEdit",
                                              "admin/cmscontenttype/editmeta/{typeid}/{id}",
                                              new { controller = "CmsContentType", action = "EditMeta" },
                                              new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                 "CmsAdmin_MediaPicker",
                 "admin/mediapicker",
                 new { controller = "CmsMediaPicker", action = "Index" },
                 new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                 "CmsAdmin_MediaPicker_Load",
                 "admin/mediapicker/load",
                 new { controller = "CmsMediaPicker", action = "Load" },
                 new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                 "CmsAdmin_MediaPicker_Browse",
                 "admin/mediapicker/browse",
                 new { controller = "CmsMediaPicker", action = "Browse" },
                 new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                 "CmsAdmin_MediaPicker_SaveMediaContent",
                 "admin/mediapicker/savemediacontent",
                 new { controller = "CmsMediaPicker", action = "SaveMediaContent" },
                 new { isInAdmin = new CmsAdminAreaRouteConstraint() });

            context.CmsAdministrationMapRoute(
                "CmsAdmin_Generic",
                "admin/{controller}/{action}/{id}",
                new { controller = "CmsAdminHome", action = "Index", id = UrlParameter.Optional },
                new { isInAdmin = new CmsAdminAreaRouteConstraint() });

        }
    }
}